<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .container {
            width: 400px;
            margin: 0 auto;
        }

        h1 {
            text-align: center;
            padding: 20px 0;
        }
        
        p {
            color: #666;
            text-align: center;
            font-size: 14px;
            padding: 10px 0;
        }

        .row {
            height: 40px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        span {
            width: 100px;
            line-height: 40px;
        }

        .edit {
            width: 200px;
            height: 30px;
        }

        .submit{
            width: 304px;
            height: 40px;
            color: white;
            background-color: rgb(85, 163, 85);
            border: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>留言墙</h1>
        <p>输入后点击提交,会将信息显示在表格中</p>
        <div class="row">
            <span>谁：</span>
            <input class="edit" type="text">
        </div> 
        <div class="row"> 
            <span>对谁：</span>
            <input class="edit" type="text">
        </div> 
        <div class="row">
            <span>说什么：</span>
            <input class="edit" type="text">
        </div> 
        <div class="row">
            <input type="button" value="提交" class="submit">
        </div>
    
    </div>
</body>

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>

<script>
    //给提交按钮注册点击事件
    var submit = document.querySelector('.submit');
    submit.onclick = function () {
        //获取到编辑框的内容
        var edits=document.querySelectorAll('.edit');
        var from=edits[0].value;
        var to=edits[1].value;
        var message=edits[2].value;
        if(from==' '||to==' '|| message==' '){
            return;
        }
        //2. 构造html元素
        var row = document.createElement('div');
        row.className='row';
        row.innerHTML=from+'对'+to+'说：'+message;
        //3.把构造好的元素添加进去
        var container = document.querySelector('.container');
        container.appendChild(row);
        //4.同时清理之前输入框的脏数据
        for(var i=0;i<3;i++){
            edits.value=' ';
        }
        let body={
            "from":from,
            "to":to,
            "message":message
        };
        $.ajax({
            type:"post",
    <!--        url:"messageWall/message",-->
            url:"message",
            contentType:"application/json;charset=utf8",
            data:JSON.stringify(body),
            success:function(body){
            //预期body中返回ok
                console.log(body);
            }
        });
    }
    //在页面加载的时候，获取get请求，从服务器是上拿数据
    $.ajax({
        type:"get",
        url:"message",
        success:function(body){
            //1.遍历数组，取出每个js对象
            //2.根据这里的js对戏那个构造出页面元素，显示在页面上
            let container=document.querySelector(".container");
            for(let i=0;i<body.length;i++){
                let message=body[i];
                // 此处message对象，就形如
                // {
                //     from:'黑猫'，
                //     to:'白猫'，
                //     message：'喵'
                // }
            }
        }
    });
</script>
</html>